//
//  SettingDetail_ViewController.m
//  MZSystem
//
//  Created by 智障 on 2018/11/5.
//  Copyright © 2018 zerod. All rights reserved.
//

#import "SettingDetail_ViewController.h"
#import "MZSettingDetailTableViewCell.h"
#import "MZGlobleConfig.h"
#import "XZPickView.h"
@interface SettingDetail_ViewController ()<UITableViewDelegate,UITableViewDataSource,XZPickViewDelegate,XZPickViewDataSource>
@property (nonatomic ,strong) Base_CenterNavView *navView;
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) XZPickView *pickView;
//分辨率（VideoSize）
@property (nonatomic,strong) NSString *  videoSize;
//FPS 帧率（frame per second）
@property (nonatomic,strong) NSString * videoFrameRate;
//视频 码率（BitRate）、
@property (nonatomic,strong) NSString * videoBitRate;

@property (nonatomic,strong) NSMutableArray *dataSource;


@end

@implementation SettingDetail_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.videoSize = [NSString stringWithFormat:@"%.0f*%.0f",[MZGlobleConfig defaultManager].videoSize.width,[MZGlobleConfig defaultManager].videoSize.height];
    self.videoFrameRate = [NSString stringWithFormat:@"%ld",[MZGlobleConfig defaultManager].videoFrameRate];
    self.videoBitRate = [NSString stringWithFormat:@"%ld",[MZGlobleConfig defaultManager].videoBitRate];
//    ,@"1920*1080"
    self.dataSource = @[@[@"768*432",@"1024*576",@"1280*720"],@[@"15",@"30",@"45",@"60"],@[@"512",@"800",@"1000",@"1500",@"2000",@"2500",]].mutableCopy;
    [self.view addSubview:self.navView];
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [@[@1,@1,@6][section] integerValue];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [@[@150,@60,@50][indexPath.section] floatValue];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [@[@0.1,@0.1,@50][section] floatValue];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        UIView * titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScrW, 50)];
        titleView.backgroundColor = [UIColor whiteColor];
        
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = [UIColor colorWithRed:66/255.0 green:74/255.0 blue:96/255.0 alpha:1];
        [titleLabel setText:@"视频码率"];
        [titleView addSubview:titleLabel];
        
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(10, 49.5, ScrW - 10, 0.5)];
        line.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
        [titleView addSubview:line];
        
        return titleView;
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MZSettingDetailTableViewCell * cell = (MZSettingDetailTableViewCell *)[[[NSBundle mainBundle]loadNibNamed:@"MZSettingDetailTableViewCell" owner:self options:nil] firstObject];
        NSArray * firstArr = [self.dataSource firstObject];
        NSInteger selectedInter = [firstArr indexOfObject:self.videoSize];
        UIButton * btn = (UIButton *)[cell.contentView viewWithTag:selectedInter + 1];
        btn.selected = YES;
        [cell.firstBtn addTarget:self action:@selector(choosevideoSize:) forControlEvents:UIControlEventTouchUpInside];
        [cell.secondBtn addTarget:self action:@selector(choosevideoSize:) forControlEvents:UIControlEventTouchUpInside];
        [cell.thirdBtn addTarget:self action:@selector(choosevideoSize:) forControlEvents:UIControlEventTouchUpInside];
        [cell.forthBtn addTarget:self action:@selector(choosevideoSize:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else if (indexPath.section == 1){
        MZSettingDetailTableViewCell * cell = (MZSettingDetailTableViewCell *)[[NSBundle mainBundle]loadNibNamed:@"MZSettingDetailTableViewCell" owner:self options:nil][1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.zhenlvLabel.text = [NSString stringWithFormat:@"%@帧/S",self.videoFrameRate];
        return cell;
    }else{
        MZSettingDetailTableViewCell * cell =  (MZSettingDetailTableViewCell *)[[NSBundle mainBundle]loadNibNamed:@"MZSettingDetailTableViewCell" owner:self options:nil][2];
        NSArray * firstArr = [self.dataSource lastObject];
        [cell.malvBtn setTitle:[NSString stringWithFormat:@"   %@kbps",[firstArr objectAtIndex:indexPath.row]] forState:UIControlStateNormal];

        if ([self.videoBitRate isEqualToString:[firstArr objectAtIndex:indexPath.row]]) {
            [cell.malvBtn setImage:[UIImage imageNamed:@"ic_tab_choice_normal02"] forState:UIControlStateNormal];
        }else{
           [cell.malvBtn setImage:[UIImage imageNamed:@"ic_tab_choice_normal"] forState:UIControlStateNormal];
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.pickView];
        [self.pickView show];
        __weak typeof(self) weakSelf = self;
        __weak typeof(self.pickView) weakPick = self.pickView;
        self.pickView.confirmButtonClick = ^(UIButton *button) {
            NSInteger currentRow = [weakPick selectedRowInComponent:0];
            NSArray * arr =  weakSelf.dataSource[1];
            weakSelf.videoFrameRate = [NSString stringWithFormat:@"%@",arr[currentRow]];
            [weakSelf.tableView reloadData];
        };
    }else if (indexPath.section == 2){
        NSArray * firstArr = [self.dataSource lastObject];
        self.videoBitRate = [firstArr objectAtIndex:indexPath.row];
        [self.tableView reloadData];
        
    }
}





-(NSInteger)pickerView:(XZPickView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSArray * arr =  self.dataSource[1];
    return arr.count;
}

-(NSString *)pickerView:(XZPickView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSArray * arr =  self.dataSource[1];
    return [NSString stringWithFormat:@"%@帧/S",arr[row]];
}

-(NSInteger)numberOfComponentsInPickerView:(XZPickView *)pickerView{
    return 1;
}


- (void)choosevideoSize:(UIButton *)sender {
    NSArray * firstArr = [self.dataSource firstObject];
    self.videoSize = [firstArr objectAtIndex:sender.tag - 1];
    [self.tableView reloadData];
}

-(XZPickView *)pickView{
    if(!_pickView){
        _pickView = [[XZPickView alloc]initWithFrame:[UIScreen mainScreen].bounds title:@"请选择"];
        _pickView.delegate = self;
        _pickView.dataSource = self;
    }
    return _pickView;
}

- (Base_CenterNavView *)navView{
    if (!_navView) {
        _navView = [Base_loadXibView loadFromXib:@"Base_CenterNavView" withCls:[Base_CenterNavView class]];
        [_navView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SafeAreaTopHeight)];
        _navView.navTitle.text = @"音频视频质量设置";
        _navView.rightbutton.hidden = NO;
        [_navView.rightbutton setTitle:@"保存" forState:UIControlStateNormal];
        WS(weakSelf)
        _navView.clickBack = ^{

            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        _navView.clickRightBtn = ^{
            [MZGlobleConfig defaultManager].videoSize = CGSizeMake([[[weakSelf.videoSize componentsSeparatedByString:@"*"] firstObject] floatValue], [[[weakSelf.videoSize componentsSeparatedByString:@"*"] lastObject] floatValue]);
            [MZGlobleConfig defaultManager].videoFrameRate = [weakSelf.videoFrameRate integerValue];
            [MZGlobleConfig defaultManager].videoBitRate = [weakSelf.videoBitRate integerValue];
            [weakSelf.navigationController popViewControllerAnimated:YES];

        };
    }
    return _navView;
}
-(UITableView*)tableView{
    if(!_tableView){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, ScrH - SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.showsHorizontalScrollIndicator=NO;
        _tableView.backgroundColor = RGB(239, 239, 239);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}




@end
